package unq.tpi.persistencia.performanceEj.daos;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.classic.Session;

import unq.tpi.persistencia.performanceEj.model.Department;
import unq.tpi.persistencia.performanceEj.model.Employee;
import unq.tpi.persistencia.performanceEj.model.Salary;
import unq.tpi.persistencia.util.SessionManager;

public class EmployeeDAO {

	public Employee getByName(final String name, final String lastName) {
		Session session = SessionManager.getSession();
		return (Employee) session
				.createQuery(
						"from Employee where firstName = :name and lastName = :lastName")
				.setParameter("name", name).setParameter("lastName", lastName)
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Employee> getAll() {
		Session session = SessionManager.getSession();
		Criteria criteria = session.createCriteria(Employee.class);
		return criteria.list();
	}

	public List<Employee> maximosSalarios() {
		Session session = SessionManager.getSession();
		List<Employee> setMaxResults = (List<Employee>) session.createQuery(
				"SELECT e FROM Employee e JOIN e.salaries s WHERE s.to = '9999-01-01' ORDER BY s.amount DESC").setMaxResults(10).list();
		return setMaxResults;

	}
	


	public Employee getByCode(int id) {
		Session session = SessionManager.getSession();

		return (Employee) session.load(Employee.class, id);
	}

	public Double getSalario(Employee emp) {
		Session session = SessionManager.getSession();
		return (Double) session.createQuery(
				"SELECT s.amount FROM  Employee e join e.salaries s where e = ? and s.to = '9999-01-01'").setEntity(0,emp).uniqueResult();
		
	}
	
}
